package cn.itcast.consumer.web;

import cn.itcast.consumer.client.UserClient;
import cn.itcast.consumer.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 虎哥
 */
@RestController
@RequestMapping("consumer")
//@DefaultProperties(defaultFallback = "defaultFallback")
public class ConsumerController {

    @Autowired
    private UserClient userClient;

    @GetMapping("{id}")
    public User queryById(@PathVariable("id") Long id) {
        return userClient.queryById(id);
    }

   /* @Autowired
    private RestTemplate restTemplate;

    @GetMapping("{id}")
    public User queryById(@PathVariable("id") Long id) {
        String url = "http://user-service/user/" + id;
        return restTemplate.getForObject(url, User.class);
    }*/


//    @HystrixCommand(commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000"),
//            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
//            @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "15000"),
//            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "70")
//    })
    /*@HystrixCommand
    @GetMapping("{id}")
    public String queryById(@PathVariable("id") Long id) {
        // 人为控制成功或失败
        if(id == 1){
            throw new RuntimeException("服务器不想跟你说话，并且抛出一个异常！");
        }

        String url = "http://user-service/user/" + id;
        // 发起一个请求，这个请求回被负载均衡的拦截器拦截
        return restTemplate.getForObject(url, String.class);
    }*/

//    public String defaultFallback(){
//        return "别挤了，服务器要被挤爆了！";
//    }


    /*@Autowired
    private DiscoveryClient discoveryClient;
    int count = 0;
    *//**
     * 查询用户，通过远程调用来查询数据
     * @param id
     * @return
     *//*
    @GetMapping("{id}")
    public User queryById(@PathVariable("id") Long id){

        // 根据服务名称，获取服务列表
        List<ServiceInstance> instanceList = discoveryClient.getInstances("user-service");
        // 利用负载均衡的策略，从集合中选取一个服务实例
        // int index = new Random().nextInt(instanceList.size());
        count = (count + 1) % instanceList.size();
        ServiceInstance instance = instanceList.get(count);
        // 获取ip和端口
        String host = instance.getHost();
        int port = instance.getPort();

        String url = String.format("http://%s:%d/user/%d", host, port, id);
        return restTemplate.getForObject(url, User.class);
    }*/

    /**
     * 查询用户，通过远程调用来查询数据
     * @param id
     * @return
     */
/*    @GetMapping("{id}")
    public User queryById(@PathVariable("id") Long id){
        String url = "http://localhost:8081/user/" + id;
        return restTemplate.getForObject(url, User.class);
    }*/
}
